<?php
if(!defined('IN_WOGU')) {
    exit('Access Denied');
}

Loader::loadClass('Db_Interface');

class Db_Factory
{
    static protected $_config = null;
    static protected $_dblist = array();

    static public function init() {
        self::$_config = Loader::loadConfig('database');
    }

    static public function getConfig() {
        return self::$_config;
    }

    static function getDatabase($dbname = '', $section = 'default') {
        $dbname = $dbname == '' ? self::$_config[$section]['database'] : $dbname;

        if(!isset(self::$_dblist[$section][$dbname])) {
            self::$_dblist[$section][$dbname] = self::_connect($dbname, $section);
        }

        return self::$_dblist[$section][$dbname];
    }

    static function _connect($dbname, $section) {
        $config = self::$_config[$section];
        $config['database'] = $dbname;
        $db = Loader::loadClass($config['dbtype'], true);
        $db->open($config);
        return $db;
    }
}